const shopCar = new Vue ({
  el : "#shopCar",
  data : {
    goods : [{
      id : 1,
      name : "算法导论",
      pubDate : "2006-9",
      price : 85,
      num : 1
    },{
      id : 2,
      name : "JavaScript高级程序设计",
      pubDate : "2006-9",
      price : 85,
      num : 1
    },{
      id : 3,
      name : "JavaScriptDOM编程艺术",
      pubDate : "2006-9",
      price : 85,
      num : 1
    },{
      id : 4,
      name : "深入理解计算机原理",
      pubDate : "2006-9",
      price : 85,
      num : 1
    },{
      id : 5,
      name : "图解算法",
      pubDate : "2006-9",
      price : 85,
      num : 1
    }]
  },
  methods :{
   /*  finalPrince(price) {
      return "￥" + price.toFixed(2);
    }, */
    increment(index) {
      this.goods[index].num++
    },
    decrement(index) {
      this.goods[index].num--;
    },
    removeHandle(index) {
       this.goods.splice(index,1)
    }
  },
  // 过滤器
  filters : {
    finalPrince(price) {
      return "￥" + price.toFixed(2);
    }
  },
  computed : {
    totalPrice ()  {
      /* let sumPrice = 0
      for (let i=0;i < this.goods.length; i++) {
        sumPrice += this.goods[i].price * this.goods[i].num;
      }
      return sumPrice; */

      // 高阶函数写法
      return this.goods.reduce(function (preValue, cur) {
        return preValue + cur.price * cur.num;
      },0)
    }
  }
})